package com.liangtian.mdm.dm.jsp.common;

import com.liangtian.mdm.dm.common.enums.Type;
import org.apache.commons.lang3.StringUtils;
import com.liangtian.mdm.dm.config.model.AttrConfig;
import com.liangtian.mdm.dm.group.model.AttrGroup;
import com.liangtian.mdm.dm.model.model.Model;

import java.util.List;
import java.util.Map;

public class EditJspCreate {
    /**
     * * <p>Title: 动态拼接替换修改页面模板</p>
     *
     * @author : xiaoliu
     * @date :2016年8月8日下午2:23:20
     */
    public static void createEditPage(String formatJspStr, String readFilePath, Model model, String operType, String backPage, Map<String, Object> result_map) {
        try {
            String tableName = model.getTableName();
            String html = JspUtil.getTmpJspContent(readFilePath)  //根据路径获取自动生成的模板内容
                    .replace("{editHtml}", formatJspStr) //将占位符替换成拼接的内容
                    .replace("@R#", "\r\n");  //格式化换行等占位符
            if ("0".equals(model.getClassType()) && !StringUtils.isBlank(model.getMaintableId())) {//模型为从表
                html = html.replace("{mainIdInputContent}", "<input type='hidden' id='mainId' name='mainId' "
                        + " ng-model='mainId' value='<%= request.getParameter(\"mainId\")%>'>");
                html = html.replace("{mainContent}", "")
                        .replace("{mainEnd}", "").replace("{mainJsContent}", "");
            } else {
                boolean bool = (boolean) result_map.get("flag");
                if (bool) {//模型有从表，为主表
                    List<Model> subModelLst = (List<Model>) result_map.get("subModel");
                    html = html.replace("{mainIdInputContent}", "");
                    Map<String, String> resMap = JspUtil.spellMainContent(model, operType, subModelLst, backPage);
                    html = html.replace("{mainContent}", resMap.get("mainHead"))
                            .replace("{mainEnd}", resMap.get("mainEnd"))
                            .replace("{mainJsContent}", resMap.get("mainJs"));
                } else {//模型没有从表，非从表
                    html = html.replace("{mainIdInputContent}", "");
                    html = html.replace("{mainContent}", "")
                            .replace("{mainEnd}", "")
                            .replace("{mainJsContent}", "");
                }
            }
            html = html.replace("{formId}", tableName);
            html = html.replace("{formName}", tableName);
            //二次开发js文件的引用
//		    if("0".equals(model.getModelType())){
//		    	  html = html.replace("{secondaryJs}", "<script src=\"${basePath}/secondary/wanda/"
//		            +model.getTableName() + "/js/" + model.getTableName() + "_add.js?t=<%=System.currentTimeMillis()%>\"></script> ");
//		    }else{
//		    	  html = html.replace("{secondaryJs}", "");
//		    }
            html = html.replace("{includeJs}", tableName + "/js/" + tableName + "_" + operType + ".js")
                    .replace("{title}", model.getModelName())
                    .replace("{ngApp}", tableName + operType + "App")
                    .replace("{ngControl}", tableName + operType + "Control");
            JspUtil.writeInJsp(html, readFilePath); //将替换后的、正式的字符串重新写入生成的jsp里
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void createViewEditPage(String formatJspStr, String readFilePath, Model model,
                                          String operType, String backPage, Map<String, Object> result_map, List<AttrGroup> groupLst, List<AttrConfig> configLst) {
        String tableName = model.getTableName();
        try {
            String jspTmpContent = JspUtil.getTmpJspContent(readFilePath); // 根据路径获取自动生成的模板内容
            String html = jspTmpContent.replace("{viewMainContentRep}", formatJspStr); // 将占位符替换成拼接的内容
            html = html.replace("{shiroRep}", AddJspCreate.spellShiroRep(operType));
            html = html.replace("{linkRep}", AddJspCreate.spellLinkRep(operType));
            html = html.replace("{scriptRep}", AddJspCreate.spellScriptRep(operType));
            html = AddJspCreate.replaceTreeTabAddFormat(html, model, operType); // 格式化换行等占位符
            boolean bool = (boolean) result_map.get("flag");
            if (bool) {//说明有从表
                @SuppressWarnings("unchecked")
                List<Model> subModelLst = (List<Model>) result_map.get("subModel");
                Map<String, String> resMap = AddJspCreate.spellMainViewSubContent(model, result_map, operType, subModelLst, backPage, groupLst, configLst);
                html = html.replace("{viewSubContentRep}", resMap.get("viewSubContentRep"))
                        .replace("{viewGroupRep}", resMap.get("viewGroupRep"))
                        .replace("{navTabsRep}", resMap.get("navTabsRep"))
                        .replace("{mainJsContentRep}", resMap.get("mainJsContentRep"))
                        .replace("{mainIdInputRep}", "<input type='hidden' ng-model='mId'  id='mId'>");
            } else {
                Map<String, String> resMap = AddJspCreate.spellMainViewSubContent(model, result_map, operType, null, backPage, groupLst, configLst);
                html = html.replace("{viewSubContentRep}", "")
                        .replace("{viewGroupRep}", resMap.get("viewGroupRep"))
                        .replace("{navTabsRep}", resMap.get("navTabsRep"))
                        .replace("{mainJsContentRep}", "")
                        .replace("{mainIdInputRep}", "");
            }

            html = html.replace("{formId}", tableName).replace("{formName}", tableName);
            //二次开发js文件的引用
//				    if("0".equals(model.getModelType())){
//				    	  html = html.replace("{secondaryJs}", "<script src=\"${basePath}/secondary/wanda/"
//				            +model.getTableName() + "/js/" + model.getTableName() + "_add.js?t=<%=System.currentTimeMillis()%>\"></script> ");
//				    }else{
//				    	  html = html.replace("{secondaryJs}", "");
//				    }
            html = html.replace("{includeJs}", tableName + "/js/" + tableName + "_" + operType + ".js")
                    .replace("{title}", model.getModelName())
                    .replace("{ngApp}", tableName + operType + "App")
                    .replace("{ngControl}", tableName + operType + "Control");
            JspUtil.writeInJsp(html, readFilePath); //将替换后的、正式的字符串重新写入生成的jsp里
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * * <p>Title: 动态拼接替换修改页面模板</p>
     *
     * @author : xiaoliu
     * @date :2016年8月8日下午2:23:20
     */
    public static void createTreeTabEditPage(String formatJspStr, String readFilePath, Model model, String operType, String backPage, Map<String, Object> result_map) {
        try {
            String tableName = model.getTableName();
            String html = JspUtil.getTmpJspContent(readFilePath)  //根据路径获取自动生成的模板内容
                    .replace("{editHtml}", formatJspStr) //将占位符替换成拼接的内容
                    .replace("@R#", "\r\n");  //格式化换行等占位符
            if ("0".equals(model.getClassType()) && !StringUtils.isBlank(model.getMaintableId())) {//模型为从表
                html = html.replace("{mainIdInputContent}", "<input type='hidden' id='mainId' name='mainId' "
                        + " ng-model='mainId' value='<%= request.getParameter(\"mainId\")%>'>");
                html = html.replace("{mainContent}", "")
                        .replace("{mainEnd}", "").replace("{mainJsContent}", "");
            } else {
                boolean bool = (boolean) result_map.get("flag");
                if (bool) {//模型有从表，为主表
                    List<Model> subModelLst = (List<Model>) result_map.get("subModel");
                    html = html.replace("{mainIdInputContent}", "");
                    Map<String, String> resMap = JspUtil.spellMainContent(model, operType, subModelLst, backPage);
                    html = html.replace("{mainContent}", resMap.get("mainHead"))
                            .replace("{mainEnd}", resMap.get("mainEnd"))
                            .replace("{mainJsContent}", resMap.get("mainJs"));
                } else {//模型没有从表，非从表
                    html = html.replace("{mainIdInputContent}", "");
                    html = html.replace("{mainContent}", "")
                            .replace("{mainEnd}", "")
                            .replace("{mainJsContent}", "");
                }
            }
            html = html.replace("{formId}", tableName)
                    .replace("{formName}", tableName);
            //二次开发js文件的引用
//			    if("0".equals(model.getModelType())){
//			    	  html = html.replace("{secondaryJs}", "<script src=\"${basePath}/secondary/wanda/"
//			            +model.getTableName() + "/js/" + model.getTableName() + "_add.js?t=<%=System.currentTimeMillis()%>\"></script> ");
//			    }else{
//			    	  html = html.replace("{secondaryJs}", "");
//			    }
            html = html.replace("{includeJs}", tableName + "/js/" + tableName + "_" + operType + ".js")
                    .replace("{ApplyColumn}", JspUtil.spellColumn(model, Type.APPLY))
                    .replace("{EditColumn}", JspUtil.spellColumn(model, Type.EDIT))
                    .replace("{UpdateEditColumn}", JspUtil.spellColumn(model, Type.UPDATEEDIT))
                    .replace("{UpdateColumn}", JspUtil.spellColumn(model, Type.UPDATE))
                    .replace("{title}", model.getModelName())
                    .replace("{ngApp}", tableName + operType + "App")
                    .replace("{ngControl}", tableName + operType + "Control");
            JspUtil.writeInJsp(html, readFilePath); //将替换后的、正式的字符串重新写入生成的jsp里
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void createTreeTabViewEditPage(String formatJspStr, String readFilePath, Model model, String operType, String backPage, Map<String, Object> result_map, List<AttrGroup> groupLst, List<AttrConfig> configLst) {
        try {
            String tableName = model.getTableName();
            String html = JspUtil.getTmpJspContent(readFilePath);  //根据路径获取自动生成的模板内容
            html = html.replace("{viewMainContentRep}", formatJspStr); // 将占位符替换成拼接的内容
            html = html.replace("{shiroRep}", AddJspCreate.spellShiroRep(operType));
            html = html.replace("{linkRep}", AddJspCreate.spellLinkRep(operType));
            html = html.replace("{scriptRep}", AddJspCreate.spellScriptRep(operType));
            html = AddJspCreate.replaceTreeTabAddFormat(html, model, operType); // 格式化换行等占位符
            boolean bool = (boolean) result_map.get("flag");
            if (bool) {//说明有从表
                @SuppressWarnings("unchecked")
                List<Model> subModelLst = (List<Model>) result_map.get("subModel");
                Map<String, String> resMap = AddJspCreate.spellMainViewSubContent(model, result_map, operType, subModelLst, backPage, groupLst, configLst);
                html = html.replace("{viewSubContentRep}", resMap.get("viewSubContentRep"))
                        .replace("{viewGroupRep}", resMap.get("viewGroupRep"))
                        .replace("{navTabsRep}", resMap.get("navTabsRep"))
                        .replace("{mainJsContentRep}", resMap.get("mainJsContentRep"))
                        .replace("{mainIdInputRep}", "<input type='hidden' ng-model='mId'  id='mId'>");
            } else {
                Map<String, String> resMap = AddJspCreate.spellMainViewSubContent(model, result_map, operType, null, backPage, groupLst, configLst);
                html = html.replace("{viewSubContentRep}", "")
                        .replace("{viewGroupRep}", resMap.get("viewGroupRep"))
                        .replace("{navTabsRep}", resMap.get("navTabsRep"))
                        .replace("{mainJsContentRep}", "")
                        .replace("{mainIdInputRep}", "");
            }
            html = html.replace("{formId}", tableName)
                    .replace("{formName}", tableName);
//		    if("0".equals(model.getModelType())){
//		    	  html = html.replace("{secondaryJs}", "<script src=\"${basePath}/secondary/wanda/"
//		            +model.getTableName() + "/js/" + model.getTableName() + "_add.js?t=<%=System.currentTimeMillis()%>\"></script> ");
//		    }else{
//		    	  html = html.replace("{secondaryJs}", "");
//		    }
            html = html.replace("{includeJs}", tableName + "/js/" + tableName + "_" + operType + ".js")
                    .replace("{ApplyColumn}", JspUtil.spellColumn(model, Type.APPLY))
                    .replace("{EditColumn}", JspUtil.spellColumn(model, Type.EDIT))
                    .replace("{UpdateEditColumn}", JspUtil.spellColumn(model, Type.UPDATEEDIT))
                    .replace("{UpdateColumn}", JspUtil.spellColumn(model, Type.UPDATE))
                    .replace("{title}", model.getModelName())
                    .replace("{ngApp}", tableName + operType + "App")
                    .replace("{ngControl}", tableName + operType + "Control");
            JspUtil.writeInJsp(html, readFilePath); //将替换后的、正式的字符串重新写入生成的jsp里
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
